/*
 * @lc app=leetcode.cn id=2 lang=javascript
 *
 * [2] 两数相加
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
  let l3head = new ListNode()
  let l3 = l3head
  let addone = 0
  while (l1 || l2) {
    l3.val = addone
    l3.val += l1?.val | 0
    l3.val += l2?.val | 0
    // l3.val += l1 ? l1.val : 0
    // l3.val += l2 ? l2.val : 0
    // addone = parseInt(l3.val / 10)
    addone = (l3.val / 10) | 0
    l3.val %= 10
    
    l1 = l1?.next
    l2 = l2?.next
    if (l1 || l2) {
      l3.next = new ListNode()
      l3 = l3.next
    }
  }
  if (addone) {
    l3.next = new ListNode(addone)
  }
  return l3head
}

// let a = null;
// console.log(a);
// console.log(a | 0);
// console.log(a?.next);
// console.log(a?.next | 0);
// console.log(a?.next || 0);

// @lc code=end

